package com;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class EffectMgr {
    //技能作用单位，比如角色，建筑
    public static interface  Unit
    {
    }

    public static void DoEffect(ArrayList<Object> cfg, Unit caster, Unit target) {
        if (cfg == null || cfg.isEmpty()) {
            Log.Error("Invalid cfg");
            return;
        }
        String methodName = cfg.get(0).toString();

        try {
            Method method = SkillEffect.class.getDeclaredMethod(methodName, ArrayList.class, Unit.class, Unit.class);
            if (method == null) {
                Log.Error("Method not found:", methodName);
            }
            
            try {
                method.invoke(null, cfg, caster, target);
            } catch (Exception e) {
                Log.Error("Failed to invoke method:", methodName, e);
            }
        } catch (NoSuchMethodException e) {
            Log.Error("Method not found:", methodName);
        }
    }
 
    public static boolean CheckCfg(ArrayList<Object> cfg)
    {
        if (cfg == null || cfg.isEmpty()) {
            Log.Error("Invalid cfg");
            return false;
        }
        String methodName = cfg.get(0).toString();

        try {
            Method method = SkillEffect.class.getDeclaredMethod(methodName, ArrayList.class, Unit.class, Unit.class);
            if (method == null) {
                Log.Error("Method not found:", methodName);
                return false;
            }
            try {
                Method checkMethod = SkillEffect.class.getDeclaredMethod("Check" + methodName, ArrayList.class);
            
                return (boolean) checkMethod.invoke(null, cfg);
            } catch (Exception e) {
                Log.Error("Method not found:", "Check" + methodName, e);
            }
        } catch (NoSuchMethodException e) {
            Log.Error("Method not found:", methodName, e);
        }
        return false;
    }
}
